﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using BidFunLib.DAO;
using BidFunLib.Entity;
using NHibernate;
using BidFunLib.EntityHelper;
using BidFunLib.BidWeb;

namespace BidFunLib.Services
{
    /// <summary>
    /// 
    /// </summary>
    public class StatsBidSaleManager
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(StatsBidSaleManager));

        /// <summary>
        /// The dao of this manager
        /// </summary>
        private StatsBidSaleDAO dao = new StatsBidSaleDAO();

        /// <summary>
        /// Singleton instance
        /// </summary>
        private static StatsBidSaleManager instance;

        /// <summary>
        /// Retreive singleton of this class
        /// </summary>
        public static StatsBidSaleManager Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new StatsBidSaleManager();
                }
                return StatsBidSaleManager.instance;
            }
            set
            {
                StatsBidSaleManager.instance = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private StatsBidSaleManager()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="statsBidSale"></param>
        /// <returns></returns>
        public StatsBidSale saveOrUpdate(StatsBidSale statsBidSale)
        {
            return dao.saveOrUpdate(statsBidSale);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="bidLogger"></param>
        public StatsBidSale calculateStatsBidSale(BidSale bidSale, Dictionary<String, BidUserWeb> usersWithBid)
        {
            log.Info("CalculateStatsBidSale " + bidSale.Label);
            StatsBidSale statsBidSale = new StatsBidSale();

            BidLogger winningBidLogger = BidSaleHelper.Instance.getWinningBidLogger(bidSale);
            if (winningBidLogger != null)
            {
                // amount donnée par la dernière enchere
                statsBidSale.Amount = winningBidLogger.AmountAsDouble;
                statsBidSale.IdBidUser = winningBidLogger.IdBidUser;
                statsBidSale.WinDateTime = winningBidLogger.DateTime;
                statsBidSale.IdBidOperatorType = winningBidLogger.IdBidOperatorType;
                statsBidSale.NbUsersConnected = winningBidLogger.NbUserConnected;
                statsBidSale.IdBidObject = bidSale.IdBidObject;
                statsBidSale.IdBidSale = bidSale.IdBidSale;
                statsBidSale.IdDay = 1; // TODO DayManager.Instance.getDayByDate(winningBidLogger.DateTime);
                statsBidSale.IdStatsBidSale = -1;

                statsBidSale.NbBidsByBot = BidSaleHelper.Instance.getNbBidByBot(bidSale);
                statsBidSale.NbBidsManual = BidSaleHelper.Instance.getNbBidManual(bidSale);
                statsBidSale.NbPointsPaidByWinner = BidSaleHelper.Instance.getNbPointsPaidByWinner(bidSale);
                statsBidSale.NbUsersLogsOnThisSale = usersWithBid.Count();
                if (usersWithBid.Count != 0)
                {
                    statsBidSale.MeanPointsPaidByUsers = (Double)((BidSaleHelper.Instance.getNbBidByBot(bidSale) + BidSaleHelper.Instance.getNbBidManual(bidSale)) / usersWithBid.Count);
                }
                if (bidSale.BidObject.RealPriceAsDouble != 0)
                {
                    statsBidSale.PsychologicRatio = 100 * (BidSaleHelper.Instance.getNbPointsPaidByWinner(bidSale) * 0.75 + statsBidSale.Amount) / bidSale.BidObject.RealPriceAsDouble;
                }
                statsBidSale.RealPrice = bidSale.BidObject.RealPriceAsDouble;

                statsBidSale.SaleDurationInSeconds = (Int32)((bidSale.EndDateTime - bidSale.StartDateTime).TotalSeconds);

                return dao.saveOrUpdate(statsBidSale);

            }
            else
            {
                // no winner
                return null;
            }

            
        }
    }
}
